package com.alianzamedica.controllers.prescription;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.web.tools.Converter;

import com.alianzamedica.businessobject.Doctor;
import com.alianzamedica.businessobject.Prescription;
import com.alianzamedica.businessobject.Tag;
import com.alianzamedica.controllers.constants.ActionConstants;
import com.alianzamedica.models.TagModel;

/**
 * @author Carlos
 * 
 */
public class ShowTagListAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		Doctor doctor = (Doctor) session.getAttribute("doctor");

		Integer pacienteId = Converter.string2Integer(request
				.getParameter("pacienteId"));

		if (pacienteId != null) {
			Prescription prescription = (Prescription) session
					.getAttribute("prescription");
			prescription.setPatientId(pacienteId);
			session.setAttribute("prescription", prescription);
		}
		Integer doctorId = doctor.getId();
		Integer pageNum = Converter.string2Integer(request
				.getParameter("pageNum"));
		if (pageNum == null) {
			pageNum = 1;
		}
		TagModel model = new TagModel();
		Integer totalPages = model.getTotalPagesFromUnusedTags(doctorId,
				ActionConstants.PAGE_SIZE);
		ArrayList<Tag> tags = model.getUnusedTags(doctorId, pageNum,
				ActionConstants.PAGE_SIZE);
		request.setAttribute("totalPages", totalPages);
		request.setAttribute("tags", tags);

		return mapping.findForward("success");
	}

}
